import {
  queryGetCreateTableDDLReq,
  queryGetDatabasesReq,
  queryGetTagetTableFieldsReq,
  queryGetTargetTablesReq,
  queryGetTaskByIdReq,
  queryLogReq,
  queryPreviewReq,
  queryReRunTaskReq,
  querySaveTargetTableReq,
  querySaveTaskReq,
  queryStopReq,
  queryTestReq,
} from './api'

export default {
  namespace: 'offlineHttpsApiCreate',
  state: {
    // formData: {},
    // viewDetailSta: false,
  },
  effects: {
    * queryTest({ params, cb }, { call, put }) {
      const res = yield call(queryTestReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryPreview({ params, cb }, { call, put }) {
      const res = yield call(queryPreviewReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryGetDatabases({ params, cb }, { call, put }) {
      const res = yield call(queryGetDatabasesReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTargetTables({ params, cb }, { call, put }) {
      const res = yield call(queryGetTargetTablesReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetCreateTableDDL({ params, cb }, { call, put }) {
      const res = yield call(queryGetCreateTableDDLReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    // 保存每一步骤里的数据
    * saveFormData({ params, cb }, { call, put }) {
      yield put({ type: 'save', payload: params })
      cb && cb()
    },
    * querySaveTargetTable({ params, cb }, { call, put }) {
      const res = yield call(querySaveTargetTableReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTagetTableFields({ params, cb }, { call, put }) {
      const res = yield call(queryGetTagetTableFieldsReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * querySaveTask({ params, cb }, { call, put }) {
      const res = yield call(querySaveTaskReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTaskById({ params, cb }, { call, put }) {
      const res = yield call(queryGetTaskByIdReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryReRunTask({ params, cb }, { call, put }) {
      const res = yield call(queryReRunTaskReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryLog({ params, cb }, { call, put }) {
      const res = yield call(queryLogReq, params)
      if (cb) {
        cb(res)
      }
    },
    * queryStop({ params, cb }, { call, put }) {
      const res = yield call(queryStopReq, params)
      if (cb) {
        cb(res)
      }
    },
    // * toggleViewDetailSta({ params, cb }, { call, put }) {
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       viewDetailSta: params.viewDetailSta,
    //     },
    //   })
    // },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
